{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0.相似矩阵及二次型\n",
    "## 0.1.环境、库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import itertools\n",
    "from functools import reduce\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体   \n",
    "plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题\n",
    "x, y, z, t = sp.symbols('x y z t')\n",
    "k, m, n = sp.symbols('k m n', integer=True)\n",
    "f, g, h = sp.symbols('f g h', cls=sp.Function)\n",
    "sp.init_printing()\n",
    "# common tool function\n",
    "def getM(var_name,r,c):\n",
    "    \"\"\"\n",
    "    make a sympy s matrix\n",
    "    :param var_name:the symbol name\n",
    "    :param r:matrix s rows\n",
    "    :param c:matrix s cols\n",
    "    \"\"\"\n",
    "    if r>1 and c>1:\n",
    "        ss=sp.symbols(\"{0}_{{{{0:{1}}}{{0:{2}}}}}\".format(var_name,r,c))        \n",
    "    elif r==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,c))\n",
    "    elif c==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,r))\n",
    "    else:\n",
    "        raise Exception('invalid input parameter！')\n",
    "    return sp.Matrix([ss[i*c:(i+1)*c] for i in range(r)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.向量的內积、长度及正交性\n",
    "## 1.1.向量的內积\n",
    "**定义:**设有$n$维向量$x=\\begin{bmatrix}x_1\\\\\\vdots\\\\x_n\\end{bmatrix},y=\\begin{bmatrix}y_1\\\\\\vdots\\\\y_n\\end{bmatrix},$<br>\n",
    "$令x_1y_1+\\dots+x_ny_n=\\begin{bmatrix}x_1&\\dots&x_n\\end{bmatrix}\\begin{bmatrix}y_1\\\\\\vdots\\\\y_n\\end{bmatrix}=x^Ty$<br>\n",
    "则称$[x,y]为向量x和y的{\\color{red}{內积}},[x,y]=x^Ty$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "內积具有下列性质(其中$x,y,z为n维向量,\\lambda为实数$):\n",
    ">* 对称性:$[x,y]=[y,x]$.\n",
    "* 线性性质:$[\\lambda x,y]=\\lambda[x,y]\\\\ [x+y,z]=[x,z]+[y,z]$.\n",
    "* 当$x=0$(零向量)时,$[x,x]=0$;<br>当$x\\not=0$(零向量)时,$[x,x]>0$<br>\n",
    "* 施瓦兹(Schwarz)不等式:${[x,y]}^2\\leq [x,x][y,y]$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2.线段的长度\n",
    "二维空间点$x$坐标为$(x_1,x_2)$,则$x$点距原点的距离为$\\sqrt{x_1^2+x_2^2}$\n",
    "\n",
    "三维维空间点$x$坐标为$(x_1,x_2,x_3)$,则$x$点距原点的距离为$\\sqrt{x_1^2+x_2^2+x_3^2}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3.向量的长度\n",
    "**定义:**令$\\sqrt{[x,x]}=\\sqrt{x_1^2+\\dots+x_n^2}\\geq 0$,称$\\|x\\|$为$n$维向量$x$的长度(或范数).当$\\|x\\|=1$时,称$x$为单位向量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量的长度具有下列性质:\n",
    ">* 非负性:$当x=0(零向量)时,\\|x\\|=0;\\\\当x\\not=0(零向量)时,\\|x\\|>0$.\n",
    "* 齐次性:$\\|\\lambda x\\|=|\\lambda|\\cdot\\|x\\|.$\n",
    "* 三角不等式:$\\|x+y\\|\\leq\\|x\\|+\\|y\\|$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.4.向量的正交性\n",
    "施瓦兹(Schwarz)不等式:${[x,y]}^2\\leq [x,x][y,y]$.<br>\n",
    "当$x\\not=0且y\\not=0$时,$\\left |\\frac{[x,y]}{\\|x\\|\\cdot\\|y\\|}\\right |\\leq 1$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**当$x\\not=0且y\\not=0$时,把$\\theta=arccos\\frac{[x,y]}{\\|x\\|\\cdot\\|y\\|}$,称为$n$维向量$x和y$的夹角.当$[x,y]=0,称向量x和y正交.$\n",
    "\n",
    "**结论:**若$x=0$,则$x$与任何向量都正交."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**两两正交的非零向量组成的向量为正交向量组."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**若$n$维向量$a_1,\\dots,a_r$是一组两两正交的非零向量,则$a_1,\\dots,a_r$线性无关.\n",
    "\n",
    "**证明:**设$k_1a_1+\\dots+k_ra_r=0(零向量)$,那么<br>\n",
    "$0=[a_1,0]=[a_1,k_1a_1+\\dots+k_ra_r]\\\\=k_1[a_1,a_1]+\\dots+k_r[a_1,a_r]\\\\k_1[a_1,a_1]+0+\\dots+0\\\\=k_1{\\|a_1\\|}^2$<br>\n",
    "$\\Rightarrow k_1=0.$<br>\n",
    "同理可证,$k_2=\\dots=k_r=0.$<br>\n",
    "综上所述,根据向量组线性相关的定义$\\Rightarrow a_1,\\dots,a_r$线性无关."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**$n$维向量$e_1,\\dots,e_r$是向量空间$V\\in\\mathbb R^n$中的向量,满足\n",
    ">* $e_1,\\dots,e_r$是向量空间$V\\in\\mathbb R^n$中的一个基(最大无关组);\n",
    "* $e_1,\\dots,e_r$两两正交;\n",
    "* $e_1,\\dots,e_r$都是单位向量;\n",
    "\n",
    "则称$e_1,\\dots,e_r$是$V$的一个${\\color{red}{规范正交基}}$.\n",
    "\n",
    "**例:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}1\\\\0\\\\0\\\\0\\end{matrix}\\right] & \\left[\\begin{matrix}0\\\\1\\\\0\\\\0\\end{matrix}\\right] & \\left[\\begin{matrix}0\\\\0\\\\1\\\\0\\end{matrix}\\right] & \\left[\\begin{matrix}0\\\\0\\\\0\\\\1\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡1⎤  ⎡0⎤  ⎡0⎤  ⎡0⎤⎤\n",
       "⎢⎢ ⎥  ⎢ ⎥  ⎢ ⎥  ⎢ ⎥⎥\n",
       "⎢⎢0⎥  ⎢1⎥  ⎢0⎥  ⎢0⎥⎥\n",
       "⎢⎢ ⎥  ⎢ ⎥  ⎢ ⎥  ⎢ ⎥⎥\n",
       "⎢⎢0⎥  ⎢0⎥  ⎢1⎥  ⎢0⎥⎥\n",
       "⎢⎢ ⎥  ⎢ ⎥  ⎢ ⎥  ⎢ ⎥⎥\n",
       "⎣⎣0⎦  ⎣0⎦  ⎣0⎦  ⎣1⎦⎦"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xe=sp.eye(4)\n",
    "xes=sp.Matrix([[xe[:,i]] for i in range(xe.shape[1])]).T\n",
    "xes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "是$\\mathbb R^4$的一个规范正交基."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**设$e_1,\\dots,e_r$是向量空间$V$中的一个正交基,则$V$中的任意一个向量可以唯一表示为$x=\\lambda_1e_1+\\dots+\\lambda_re_r$<br>\n",
    "于是$\\lambda_i=\\frac{[x,e_i]}{[e_i,e_i]}=\\frac{[x,e_i]}{{\\|e_i\\|}^2},i=1,\\dots,r$<br>\n",
    "特别地,若$e_1,\\dots,e_r$是$V$的一个规范正交基,则<br>\n",
    "$\\lambda_i=[x,e_i],i=1,\\dots,r$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.5.求规范正交基的方法\n",
    "**步骤:**\n",
    ">1. 基\n",
    "2. 正交基\n",
    "3. 规范正交基"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5.1.第一步:正交化-施密特正交化过程\n",
    "设$a_1,\\dots,a_r$是向量空间$V$中的一个基,那么令<br>\n",
    "$b_1=a_1\\\\b_2=a_2-\\frac{[b_1,a_2]}{[b_1,b_1]}b_1\\\\b_3=a_3-\\frac{[b_2,a_3]}{[b_2,b_2]}b_2-\\frac{[b_1,a_3]}{[b_1,b_1]}b_1\\\\\\vdots\\\\b_r=a_r-\\#b_1-\\dots-\\#b_{r-1}$<br>\n",
    "其中$b_1,\\dots,b_r$两两正交,并且与$a_1,\\dots,a_r$等价,即$b_1,\\dots,b_r$是向量空间$V$中的一个正交基.<br>\n",
    "特别地,$b_1,\\dots,b_k$与$a_1,\\dots,a_k$等价$(1\\leq k\\leq r)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5.2.第二步:单位化\n",
    "设$b_1,\\dots,b_r$是向量空间$V$中的一个正交基,那么令<br>\n",
    "$e_1=\\frac{1}{\\|b_1\\|}b_1,\\dots,e_r=\\frac{1}{\\|b_r\\|}b_r\\\\\\because  [e_1,e_1]=\\left | \\frac{1}{\\|b_1\\|}b_1,\\frac{1}{\\|b_1\\|}b_1 \\right |=\\frac{1}{{\\|b_1\\|}^2}[b_1,b_1]=1\\\\\\therefore \\|e_1\\|=\\sqrt{[e_1,e_1]}=1\\\\从而e_1,\\dots,e_r是向量空间V中的一个规范正交基.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**如下矩阵$A$向量组，试用施密特正交化过程把这组向量规范正交化."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}1\\\\2\\\\-1\\end{matrix}\\right] & \\left[\\begin{matrix}-1\\\\3\\\\1\\end{matrix}\\right] & \\left[\\begin{matrix}4\\\\-1\\\\0\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡1 ⎤  ⎡-1⎤  ⎡4 ⎤⎤\n",
       "⎢⎢  ⎥  ⎢  ⎥  ⎢  ⎥⎥\n",
       "⎢⎢2 ⎥  ⎢3 ⎥  ⎢-1⎥⎥\n",
       "⎢⎢  ⎥  ⎢  ⎥  ⎢  ⎥⎥\n",
       "⎣⎣-1⎦  ⎣1 ⎦  ⎣0 ⎦⎦"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[1,2,-1],[-1,3,1],[4,-1,0]])\n",
    "xas=sp.Matrix([[xa[i,:].T] for i in range(xa.shape[1])]).T\n",
    "xas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解:**第一步正交化:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}1\\\\2\\\\-1\\end{matrix}\\right], \\quad \\left[\\begin{matrix}- \\frac{5}{3}\\\\\\frac{5}{3}\\\\\\frac{5}{3}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}2\\\\0\\\\2\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡1 ⎤  ⎡-5/3⎤  ⎡2⎤⎞\n",
       "⎜⎢  ⎥  ⎢    ⎥  ⎢ ⎥⎟\n",
       "⎜⎢2 ⎥, ⎢5/3 ⎥, ⎢0⎥⎟\n",
       "⎜⎢  ⎥  ⎢    ⎥  ⎢ ⎥⎟\n",
       "⎝⎣-1⎦  ⎣5/3 ⎦  ⎣2⎦⎠"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xb0=xas[0]\n",
    "xb1=xas[1]-(xb0.T*xas[1])[0]/(xb0.T*xb0)[0]*xb0\n",
    "xb2=xas[2]-(xb0.T*xas[2])[0]/(xb0.T*xb0)[0]*xb0-(xb1.T*xas[2])[0]/(xb1.T*xb1)[0]*xb1\n",
    "xb0,xb1,xb2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第二步单位化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left [ \\left[\\begin{matrix}\\frac{\\sqrt{6}}{6}\\\\\\frac{\\sqrt{6}}{3}\\\\- \\frac{\\sqrt{6}}{6}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}- \\frac{\\sqrt{3}}{3}\\\\\\frac{\\sqrt{3}}{3}\\\\\\frac{\\sqrt{3}}{3}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2}\\\\0\\\\\\frac{\\sqrt{2}}{2}\\end{matrix}\\right]\\right ]$$"
      ],
      "text/plain": [
       "⎡⎡ √6 ⎤  ⎡-√3 ⎤      ⎤\n",
       "⎢⎢ ── ⎥  ⎢────⎥  ⎡√2⎤⎥\n",
       "⎢⎢ 6  ⎥  ⎢ 3  ⎥  ⎢──⎥⎥\n",
       "⎢⎢    ⎥  ⎢    ⎥  ⎢2 ⎥⎥\n",
       "⎢⎢ √6 ⎥  ⎢ √3 ⎥  ⎢  ⎥⎥\n",
       "⎢⎢ ── ⎥, ⎢ ── ⎥, ⎢0 ⎥⎥\n",
       "⎢⎢ 3  ⎥  ⎢ 3  ⎥  ⎢  ⎥⎥\n",
       "⎢⎢    ⎥  ⎢    ⎥  ⎢√2⎥⎥\n",
       "⎢⎢-√6 ⎥  ⎢ √3 ⎥  ⎢──⎥⎥\n",
       "⎢⎢────⎥  ⎢ ── ⎥  ⎣2 ⎦⎥\n",
       "⎣⎣ 6  ⎦  ⎣ 3  ⎦      ⎦"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1/sp.sqrt((i.T*i)[0])*i for i in [xb0,xb1,xb2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**如果$n$阶矩阵$A$满足$A^TA=E_n,即A^{-1}=A^T$,则称矩阵$A$为正交矩阵,简称正交阵.\n",
    "\n",
    "**证明:**<br>\n",
    "$\\because A^TA=E_n,$<br>$\\therefore a_{:,i}^Ta_{:,j}=\\begin{cases}1,i=j\\\\0,i\\not=j\\end{cases}(i,j=1,\\dots,n)$<br>$\\therefore$ 方阵$A$为正交阵的充要条件是$A$的列向量都是单位向量,且两两相交.即$A$的列向量组构成$\\mathbb R^n$的规范正交基.<br>\n",
    "又$\\therefore A^TA=E与AA^T=E$等价,<br>\n",
    "$\\therefore b_{i,;}b_{j,:}^T=\\begin{cases}1,i=j\\\\0,i\\not=j\\end{cases}(i,j=1,\\dots,n)$<br>\n",
    "$\\therefore$ 方阵$A$为正交阵的充要条件是$A$的<font color=red>行向量</font>都是单位向量,且两两相交.即$A$的<font color=red>行向量组</font>构成$\\mathbb R^n$的规范正交基."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**正交矩阵具有如下性质:**\n",
    ">* 若$A$是正交阵,则$A^{-1}$也是正交阵,且$\\left|A \\right|=1或-1$.\n",
    "* 若$A和B$是正交阵,则$AB$也是正交矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**若$P$是正交阵,则线性变换$y=Px$称为正交变换.$\\|y\\|$经过正交变换,线段的长度保持不变(从而三角形的形状保持不变),这就是正交变换的优良特性."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.方阵的特征值与特征向量\n",
    "**引言:**\n",
    ">* 纯量阵$\\lambda E$与任何同阶矩阵的乘法都满足交换律,即$(\\lambda E_n)A_n=A_n(\\lambda E_n)$.\n",
    "* 矩阵乘法一般不满足交换律,即$AB\\not=BA$.\n",
    "* 数乘矩阵与矩阵乘法都是可交换的,即$\\lambda (AB)=(\\lambda A)B=A(\\lambda B)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1.基本概念\n",
    "**定义:**设$A$是$n$阶矩阵,如果数$\\lambda 和n$维非零向量$x$满足$Ax=\\lambda x$,那么这样的数$\\lambda$称为矩阵$A$的特征值,非零向量$x$称为$A$对应的特征值$\\lambda$的特征向量.\n",
    "\n",
    "**例:**\n",
    ">$\\begin{bmatrix}3&-4\\\\2&-3\\end{bmatrix}\\begin{bmatrix}2\\\\1\\end{bmatrix}=1\\cdot\\begin{bmatrix}2\\\\1\\end{bmatrix}$<br>\n",
    "则$\\lambda=1$为$\\begin{bmatrix}3&-4\\\\2&-3\\end{bmatrix}$的特征值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$Ax=\\lambda x=\\lambda Ex\\\\\\Leftrightarrow 非零向量满足(A-\\lambda E)x=0(零向量)\\\\\\Leftrightarrow 齐次线性方程组有非零解\\\\\\Leftrightarrow 系数行列式\\left |A-\\lambda E \\right |=0$\n",
    "\n",
    "**定义:**\n",
    ">* 系数行列式$\\left |A-\\lambda E \\right |$,称为特征多项式\n",
    "* $\\left |A-\\lambda E \\right |=0$,称为特征方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2.基本性质\n",
    "**性质:** 在复数范围内$n$阶矩阵$A$有$n$个特征值(重根按重数计算).\n",
    "\n",
    "**性质:** 设$n$阶矩阵$A$的特征值为$\\lambda_1,\\dots,\\lambda_n$,则\n",
    "* $\\lambda_1+\\dots+\\lambda_n=a_{11}+\\dots+a_{nn}$\n",
    "* $\\lambda_1\\dots\\lambda_n=\\left | A \\right |$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**求解矩阵$A$的特征值和特征向量."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}3 & -1\\\\-1 & 3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡3   -1⎤\n",
       "⎢      ⎥\n",
       "⎣-1  3 ⎦"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[3,-1],[-1,3]])\n",
    "xa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解:**$A$的特征多项式为"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIEAAAAYCAYAAADdyZ7bAAAABHNCSVQICAgIfAhkiAAAA8VJREFUaIHt2UuIHFUUxvFfomjGCBFf0aBuzCRBovGFKCq0GlwI4gM32Uhr3Kkg4iZBcTZifAyKYoQswiiCjgQiZKEOSMAX0YgmiMZXYESTmRgxPsBRk9guTjVp266emuqq6Y70H4rqvufec7+6derWubfo06dPoazBdvyK/diC5V1V1GfWeRN3iBt/PjZjEid3U1ReXsQPmN9tIT3GJahhdcb6J+IwbixNUUlcir9xf7eFzAKP4S18hyn8hE/wME5JabMZE+IGT8eZImiu7FjpzLkNz+Id8Xqq4aWsjcfwMwZKkdZb/IVt2Ih1YtC2iwHbg7NbtLkssa/N4H9UBNUxRYidITuEzt+wywyCYImYBTaUJq14quICKznazkspfyTxuT7Fvgvfan9znxAzxuIcuorgGgxijhib1CCY2/T/zqTRaEnCxhIxtzaVz8FIYltXUt+t+COl/NXkPJhifwXnYGWKfRi34zp8k1tdZ2zF12JMZ8RHOKS8hHCFSJQ+9++naFiIzTMDVeWfCdJ4MPE5nGJfmdifbGF7BvtwXoF6OqUi4+tgvgiAT0sWNJIIqib/1yb/R/13ZspCVedB8ACG8JRIpGrYidNS6i9I6nzYVL5eJGHX4oyGI0sSWSYVGYNgSVJxrGRBZ4lMfBz3JH2+geNy+qvqPAgmEx/143UsnKbNVNKukVrKMdSBtiKoyBgEVzjyRLZiXPpFtjradfhoQ733cMJ04nJqGMnot85C3IIvsRcXt6m7R8ycRTGuuPFtptKuzbENv6eSc1rGvFt6ItWKvW1s+xt+r8bvGX0+jZOayi7ETXhBDGQjOzL6rbNP7AN8jK/Eplnatu+AI2NWBEWOb24WiWh5twznDawSy9CJpL/nO/RXVXxiSKzvazi1hW2uuIbdBfdZFhUZl4gT4gldWqKYG8QT+xkuwBe4C8tK7DMvi5Lz4Ra2pWJZO9OZpidpDIIa3haRX8YGx1XYhO9xvQi4h8QraTb3BuosE5l7M3PFZtHpeB8HWtS5PDlvLUdad1klguHugv2uEFvREzi3yVbfpr06p++qfK+D+3BQfDvYIJLVjWKKryVa09b6L4uksNW2cq9ws0iMR8TqqyaurV7Wao8DsUybxAcFilmc+DwgXgHN1DdetuX0X5UvCJbjOTGl/yhu6i8iKIekf/5dIBLC12asdHYZ0n51Md6u8Zqk0kVlKjyKuVdnM9dRwTzxcWRLt4X0IANiabap20KKpNVXsENinXy8+JZwcFYV9TaD+BOPixynT58+ffr0+b/wD4GPE0fxWQatAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left(- x + 3\\right)^{2} - 1$$"
      ],
      "text/plain": [
       "        2    \n",
       "(-x + 3)  - 1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(xa-x*sp.eye(2)).det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求解矩阵$A$的特征方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAVCAYAAAAU9vPjAAAABHNCSVQICAgIfAhkiAAAAglJREFUWIXt2DtoFFEUxvFfNIVlUBEbC0tBCy2sLEbBMpV1fGAKQQiChSBEEhuDIihY2CiC2AkWIaIgstGAjYUgiIpCtBALEUV84SMWdxbHzY7O7L1Zt9g/DDPMnfNx7plzz33QpxIZFgrX4//qTfdY7c9+LzQblrX5eBaTOFd4twqjuIZn+Iz3mMP+Ep1eYsTvjo+2tH0S+juJF2UCWW480abtQN72CldwAhfxLn9/FQMRzi8l6wQ/P2gfnCINhcwpkikPzg4MW5wha/Eyt9tV3d+uMYBbeI5Tagan6nC4jWn8bHn/Gufz56yiVjcZE37sPnysa5yiVnzL798TaKVkA6ZwFnc6EYgNziB25883IrVSMojLwpA/GiMSwxQ24jpuRmql5Bg2Y5sws3ZETOaM4bCwHhqJ0EnNViFbTuNejFCnwTkojOVH2I63MU4kpDmcnmI8pXCmfCovcij/7iHWpHQgAUMWr3bLrjNt7BsKU3ndmnNEqDMPsBNvatovNV9xoaRti1CH5vBEzSGX+XvmjOft97Gyoual3GZvHUeWiAk1F4FVM2cPjuMH7grFuJV5IRhFmjWt19ZAlaganPX5fblQc9oxa3FwNgl7mpnanvUYmWoFuSpDQqadTKTXDRr+sfFMdZ4zjC/C5rSXKT3PKQ6reeFMo0nsTDSNFZEa3aB5ntOnTyJ+AQgQhJlhMoK5AAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left [ 2, \\quad 4\\right ]$$"
      ],
      "text/plain": [
       "[2, 4]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve((xa-x*sp.eye(2)).det(),x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所以$A$的特征值为$\\lambda_1=2,\\lambda_2=4$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_1=2$时,对应的特征向量应满足"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFMAAAAWCAYAAAC8J6DfAAAABHNCSVQICAgIfAhkiAAAAwVJREFUWIXtmEtoFEEQhr81mqgExeD7fRCJD9ic9KJhEd8exBdBTzkoQhQF8aCCmIMSQYQgiEEvOeTi46CYg3pSUDSoIAgqihjBV1APATXqRtdD1eDaO9PbvbPrEpIfhtnp+rv6n97qrpqGIfw3bAPuAb3AL6CuvHLKgmYgDXwALgNzo4jDLU7mAx3AV+Ai8FYdDjbcBEYigbQemAos8nWyC8gAe4qpbIDjIfAbqA4zDrN0rNH702IrGsB4BiSAcWFG22RW6D1t4dxAonej0Z4A2tV23EVlkVBqPcFcVFhZIWjWwVMWThJJTE+MAU5q37O+g4agXX01OnBLrSfQMtu3Y6t2XOw4QKM+H9Ln89gj3xWmf19+MfW0qa+kT6cEfzfbyXm404E+oBvYrYNdAyo9hUZhClALjHXkl1LPAfW314W8GlkS97VTm+MgLcrPAHeA0RG8JuAV8B35s5Y6+veFi5564CrwTnmbHfxOQ/6kNHABOIZlyQdLOwM8BhY4it+X1a82gtOgInYA84BTwBdgpuMYPnDRswY4iiQr18kE2Al8y/KfspHHIMVpH/CG/JlrK7IdvFfnZyJ4XcA5o+0FEkXFhKuebLhO5jrl3kYCbYSrqA7tuNDCWQv8RKJ4AlKTpsmNhkqgH9hitJ8GbrkKcoCrHhOukxlUBcvDjLbs9lrvNRH2JcAlJHpXAh+Bw8gnqlnLjUcivMdo7yF/gnNNQD56CkVQrHeHGW2TmbZwkkAncgCyAllSIC/zANkmwpJLxnhOhLSZaEEibIOFU6geXwRz0W8zhiHqJecA19W+Cnhp2A/q/URW2yekmDajcCK50eqLQvTERb4AyEFQU9kiwgdd5H6BPKf4CagQuO6ZV5Q7yXeATdqxE5hB/K+HBiQ5bEdKo1akNJoV02+hqEaO1eqQ99yvv8NKtSrk2K1XL++5GIVkxUzWFfdwuAnZvH8gRXt9TH9xkOLfdwuudoPXbNiPRDlM5BmwCliGRFI1skwH2wFxCqkUPgN3gUdlVTOEIXjjDyOY2wB6+ZtBAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : x_{1}\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: x_{1}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx=getM('x',2,1)\n",
    "sp.solve((xa-2*sp.eye(2))*xx,xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**由上**解的基础解系$p_1=\\begin{bmatrix}1\\\\1\\end{bmatrix}$.$kp_1(k\\not=0)$也就是对应的特征向量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_2=4$时,求解过程同上."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**性质:**若$\\lambda是A$的一个特征值,则齐次线性方程组的基础解系就是对应于特征值$\\lambda$的全体特征向量的最大无关组."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设$\\lambda$是方阵$A$的特征值,证明\n",
    ">1. ${\\lambda}^2是A^2$的特征值;\n",
    "2. 当$A$可逆时,$\\frac{1}{\\lambda}是A^{-1}$的特征值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**证明1:**\n",
    ">$\\because Ax=\\lambda x\\\\\\therefore AAx=A\\lambda x=\\lambda Ax={\\lambda}^2x\\\\\\therefore {\\lambda}^2是A^2的特征值$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**证明2:**\n",
    ">$\\because Ax=\\lambda x\\\\\\therefore A^{-1}Ax=A^{-1}\\lambda x\\\\\\therefore A^{-1}x=\\frac{1}{\\lambda}Ex=\\frac{1}{\\lambda}x \\\\\\therefore \\frac{1}{\\lambda}是A^{-1}的特征值$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**结论:**若非零向量$p是A$对应于特征值$\\lambda$的特征向量,则\n",
    "> * ${\\lambda}^2是A^2的特征值$,对应的特征向量也是$p$.\n",
    "* ${\\lambda}^k是A^k的特征值$,对应的特征向量也是$p,k\\in\\mathbb{Z+}$.\n",
    "* 当$A$可逆时,$\\frac 1 \\lambda 是A^{-1}$的特征值,对应的特征向量仍然是$p$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**性质:**若$\\lambda 是A$的一个特征值,则$\\phi(\\lambda)=a_1{\\lambda}^0+\\dots+a_m{\\lambda}^m$是矩阵多项式$\\phi(A)=a_1{A}^0+\\dots+a_m{A}^m$的特征值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$\\lambda_1,\\dots,\\lambda_m$是方阵$A$的特征值,$p_1,\\dots,p_m$依次是与之对应的特征向量,如果$\\lambda_1,\\dots,\\lambda_m$各不相同,则$p_1,\\dots,p_m$线性无关."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设$\\lambda_1和\\lambda_2$是方阵$A$的不同的特征值,对应的特征向量依次为$p_1,p_2$,证明$p_1+p_2$不是$A$的特征向量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**证明:**\n",
    "如果$p_1+p_2$是$A$的特征向量,<br>\n",
    "$\\Rightarrow A(p_1+p_2)=\\lambda (p_1+p_2)=\\lambda E(p_1+p_2)$<br>\n",
    "$\\Rightarrow \\lambda_1p_1+\\lambda_2p_2=\\lambda p_1+\\lambda p_2$<br>\n",
    "$\\because \\lambda_1\\not=\\lambda_2\\Rightarrow \\lambda 无解$<br>\n",
    "$\\therefore p_1+p_2不是A的特征向量.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.相似矩阵\n",
    "**定义:**设$A,B$都是$n$阶矩阵,若有可逆矩阵$P$满足$P^{-1}AP=B$,则称$B$为矩阵$A$的相似矩阵,对$A$进行运算$P^{-1}AP$称为对$A$进行相似变换.称可逆矩阵$P$为把$A$变成B的相似变换矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**若$n$阶矩阵$A和B$相似,则$A和B$的特征多项式相同,从而$A和B$的特征值也相同.\n",
    "\n",
    "**证明:**\n",
    ">根据题意,存在可逆矩阵$P$,使得$P^{-1}AP=B$,<br>\n",
    "$\\Rightarrow B的特征多项式为$<br>\n",
    "$\\left|B-\\lambda E\\right|\\\\=\\left|P^{-1}AP -\\lambda P^{-1}P\\right|\\\\=|P^{-1}||A-\\lambda E||P|(由|P^{-1}||P|=1)\\\\=|A-\\lambda E|$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**若$n$阶矩阵$A和B$相似,则$A$的多项式$\\phi(A)和B$的多项式$\\phi(B)$相似.\n",
    "\n",
    "**证明:**设存在可逆矩阵$P$,使得$P^{-1}AP=B$,则$P^{-1}A^{\\color{red}{k}}P=B^{\\color{red}{k}}$.<br>\n",
    "$设\\phi(x)=c_0x^0+\\dots+c_mx^m,那么\\\\P^{-1}\\phi(A)P\\\\=P^{-1}(c_0A^0+\\dots+c_mA^m)P\\\\=c_0P^{-1}A^0P+\\dots+c_mP^{-1}A^mP\\\\=c_0B^0+\\dots+c_mB^m\\\\=\\phi(B).$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$n$阶矩阵$\\Lambda=diag(\\lambda_1,\\dots,\\lambda_n)$,则$\\lambda_1,\\dots,\\lambda_n$就是$A$的$n$个特征值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**若$n$阶矩阵$B和n$阶对角阵$\\Lambda=diag(\\lambda_1,\\dots,\\lambda_n)$相似,则$\\phi(B)=P^{-1}\\phi(\\Lambda)P$.从而通过计算$\\phi(\\Lambda)$可方便地计算$\\phi(B)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.对称矩阵的对角化\n",
    "**定理:**设$\\lambda_1,\\dots,\\lambda_m$是方阵$A$的特征值,$p_1,\\dots,p_m$依次是与之对应的特征向量,如果$\\lambda_1,\\dots,\\lambda_m$各不相同,则$p_1,\\dots,p_m$线性无关."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可逆矩阵$P$,满足$P^{-1}BP=\\Lambda$(对角阵),则:<br>\n",
    "$P^{-1}BP=\\Lambda\\Leftrightarrow BP=P\\Lambda$($n$阶矩阵$B$和对角阵相似的,当且仅当$B$有$n$个线性无关的特征向量.)<br>\n",
    "$\\Leftrightarrow Bp_i=\\lambda_ip_i(i=1,\\dots,n)(\\lambda_i:B的特征值,p_i:\\lambda_i对应的特征向量)$(推论:如果$B$有$n$个不同的特征值,则$B$和对角阵相似.)<br>\n",
    "$\\Leftrightarrow B(p_1,\\dots,p_n)=\\begin{bmatrix}p_1&\\dots&p_n\\end{bmatrix}\\begin{bmatrix}\\lambda_1&&\\\\&\\ddots&\\\\&&\\lambda_n\\end{bmatrix}$\n",
    "\n",
    "**说明:**当$A$的特征方程有重根时,就不一定有$n$个线性无关的特征向量,从而不一定能对角化."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$\\lambda_1和\\lambda_2$是对称矩阵$A$的特征值,$p_1,p_2$是对应的特征向量,如果$\\lambda_1\\not=\\lambda_2$,则$p_1,p_2$正交.\n",
    "\n",
    "**证明:**\n",
    ">$\\because Ap_1=\\lambda_1p_1,Ap_2=\\lambda_2p_2,\\lambda_1\\not=\\lambda_2\\\\\\therefore p_1^TA^T=\\lambda_1p^T_1\\\\\\because A=A^T\\Rightarrow p_1^TAp_2=\\lambda_1p^T_1p_2\\\\\\because Ap_2=\\lambda_2p_2\\Rightarrow p_1^TAp_2=\\lambda_2p_1^Tp_2\\\\\\therefore \\lambda_1p^T_1p_2=\\lambda_2p_1^Tp_2\\\\\\because \\lambda_1\\not=\\lambda_2\\\\\\therefore p_1^Tp_2=0,即p_1,p_2正交.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$A$为$n$阶对称阵,则必有正交阵$P$,使得$P^{-1}AP=P^TAP=\\Lambda$,其中$\\Lambda$是以$A$的$n$个特征值为对角元的对角阵(不唯一)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**设$A为n$阶对称阵,$\\lambda是A$的特征方程的$k$重根,则\n",
    "* 矩阵$A-\\lambda E$的秩等于$n-k$,\n",
    "* 恰有$k$个线性无关的特征向量与特征值$\\lambda$对应."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设$A$为如下矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}0 & -1 & 1\\\\-1 & 0 & 1\\\\1 & 1 & 0\\end{matrix}\\right], \\quad \\left[\\begin{matrix}0 & -1 & 1\\\\-1 & 0 & 1\\\\1 & 1 & 0\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡0   -1  1⎤  ⎡0   -1  1⎤⎞\n",
       "⎜⎢         ⎥  ⎢         ⎥⎟\n",
       "⎜⎢-1  0   1⎥, ⎢-1  0   1⎥⎟\n",
       "⎜⎢         ⎥  ⎢         ⎥⎟\n",
       "⎝⎣1   1   0⎦  ⎣1   1   0⎦⎠"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[0,-1,1],[-1,0,1],[1,1,0]])\n",
    "xa,xa.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求正交阵$P$,使$P^{-1}AP=\\Lambda对角阵$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解:**因为$A$是对称阵,所以$A$可以对角化.对应特征多项式为"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH4AAAAWCAYAAAAGhCi/AAAABHNCSVQICAgIfAhkiAAAAyFJREFUaIHt2V+IFWUYx/GPJhFdVVYWKARZSKgb0WVCaCAZEf258aoBgwIjvCiEKNob2Q2SFKkgIk5RF0FQN1FURlCSUGS1kBQZa5GbJpkR/THLLp730HGcc3bP7JkZL+YLA7vv++7z+z3vO/P+W1paGmAzvsCv6fkItzTqqKUWbsMGXIWrsQ1/Y3WTplqa4Wfc27SJlvo4BxtxAqsa0H8cu/E9/hAv4D48hsUN+BmWxbgHr+EbkcNxfIhNWNictWJW4TecxDEx9TfBCezF85jELnyMU/gByxryNVfuE14P4WVMiFx+SeWvYkFj7go4F8txvTB7FCtLxMlEgjeW9HFen/JtKe7TJePWxVrc6swv+zJ8J3K4syrxt5PAHbnyBeikuslZYryL50poZ+Y38P0YS3HfKagbRb518LDwsqsqgTH8gy/Fmt1lexJ+dg4x3sNLJbQz1Qz8Iynu9oK6UeRbBw8JP09WKdJJIln6vfu2veLMaWgSa3CFWOsn8C9uLqGbGc3AP4hx0UkfpJif45I+7Tvmnm8TLMKU8LS+SqGlYkc5jfuT4FtiLc/TwUH8hSNimi9rLjOagf8xxek+b2LJgPbD5NsETwhPbwxqNO30pGd7+k3JEz1t9uD8+bqfp89OCY0luB1fiZ3ydQPalsl3egj/g/p6EA+kv92Pi3orFuUaHsCfQwQ+1Kf8p56fN+H3IWLOhR24IFd2rbgJfEF0ai+fldA4LM7En+JrvKj/aaNMvqPq635sxk6x/1gn7iUqZaNYp2fE2/ZM1YKJTDWbO+Ii5xQuLqhrKt9BbBFepnBpHYIbxEXIlNgM7Rd37ytq0M5UN/CHU+wLc+VN5tuPrcLrPsUv6si5QUxx3+LyVHZXMvF6DfqZ8gO/Qlx05Fno/wucPbm6pvMt4tGk/4ncml4VY+JqcAZX5uq6155rKvaQKT/wW8SXulucvbvXnQdSzBlc09P+bMg3z91J96Q4io4XPNkoBZeLI9Axxf9OvSkZ2jtK0QIy5Qd+JZ4Sm8CjovOOi0Ecd/rXc7bkm2fc7CeD92v21NLS0tLS0lIz/wF7SBOJEHmMKwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$$- x^{3} + 3 x - 2$$"
      ],
      "text/plain": [
       "   3          \n",
       "- x  + 3⋅x - 2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(xa-x*sp.eye(3)).det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求解特征方程得矩阵$A$的特征值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFgAAAAVCAYAAADCUymGAAAABHNCSVQICAgIfAhkiAAAAflJREFUWIXt2D2IE0EYxvGfcoWNoGhxYGFlp2hr5eqVcqBo6ceVgiCCgpUQbRQbsbNSC+3sDkQbjViLB4oKIgSLQ+QQuQMRv85i5kjMJdnd3G42OfKHIcPufDz7ZJh552XMwEiw3FLeV6pmtNjuf++WV15MdGj8HHUsFCxiG47iMPZgB37iNe7G8rfgOdfCcRzAPuzFZjzAiQ5tv+NKrM9gZ6cBE8H5WqEym5yJ488LQq/hDr7F5w+xoaS5+2FO0LWEd7F+P0O/upYV3EqiXIMPYRob255P4lOc+1hJc/fDQewS/vREnwa3f2yZPMWs1dvAZ9yO9WSAetJ4hg+6rMasDNLgXvyKv78rVVECw2DwBE7F+uMqhZTBMBh8HbvxCE8q1lI4eQxuWB3r9SpZDoRzuCDE3CdzaBkZOsXB3fiIHznaz6e8P4tbeIspfM0x9siQx+CpAuc9j5t4E8f9UuDYQ0UVe/Alwdw5IdZct+YyeIMvC4faS2HlZrmO3xP29JnSVJVIni1irZzGVfzBC+GAa6chGNrKyiIYdIx8JBbCbRP2a+pbwMU8AybKvSrXpEce9Q79XmERW0vS1Y2a3lobXfrVVZSL6Ictwoq/UbWQHNSlGDxM+eBpITScTGtYMZnywQ3NnCbF54P7YRabqhaRgdZ88Jgx64h/1lSJv7M7hyMAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$\\left [ -2, \\quad 1\\right ]$$"
      ],
      "text/plain": [
       "[-2, 1]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve((xa-x*sp.eye(3)).det(),x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求得$A$的特征值$\\lambda_1=-2,\\lambda_2=\\lambda_3=1$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_1=-2$时,解方程组$(A+2E)x=0$.得基础解系$\\xi_1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANsAAAAWCAYAAABJ9ekqAAAABHNCSVQICAgIfAhkiAAABEZJREFUeJztm12IVVUUx3+3qfGjoTIIdSoNlEiLxid90GQQH4ZiCD9iSHoY0JcmsZceqofmIoiiDH6EHyjUIeZliqAoKKwHhZFmCBlD0VSqIZqpGMwPNNNRp4e1D3M4no+9z7nnnHvv7B9c5t5z9lp7/e+adfc5e+8DFoulKtgADABXgbvAkmLDsViqjjIwDvwFfAE8G9bwwQgni4Fe4AbwGTCiHFoslkmOAdORgehVoBlYaurkLWAC2FLJyCyWOuYkcA9oCjr5QITh4+rvuUpHZLHUKT8DJWBW0MmoYmtQf8cj2hxFRr+1vuMlwFHnduhEWUPUk+Z60qJD1nrdWmmIbBVAWXXeGtGmBZk4OevroEfZHjbtNABH+eqsgK9KkIfmvJhq+ctar6P8PGNquEcZLtPsoFN9fl997iN65NTF778acMhWc544TK38OWSn95Dy1WJiVGLyZm9OTNungJvAMLBZdfYt0GgYaBhzgeeARyvkrxJkrTlPplr+stT7rvL3tk7jNmRI/VEZHdLsZLtqPwGcAGaGtOsCfgP+Q4r5JU3/SRn2xKXz6jXwHaf5PeR7vAaMAV8BLySTkTlxWlYi8Y+qNutziGmY4nIHyfL3pIp7HPgU2IbnktK/ztbGZFWeAT7UDH7M834j8G9Amw5gL1Jw/cCbwDfIet7vmv2Y8gtS2LqMGrSN09wKHEASVgK2At8jev8x6CcP4rQ8DPwEfAx8nlNMReYOkuVvBCnk3cBr6th3SAEG8giyOHcT+IP4mZXXkcvNP5FfioMh7QaBI75jF1VwtYauZi9NyM15e4ZxJcFUS14jW1YkyR3o5e8V5bMfeB54SDeoXmUYNXS+DNwGTgNPIGty48g1updG4A6T1e6yHziuG1CVoKvZz1zk+1yeaXRmJNFSy8WWNHeglz93VnO1aWDblOHKkPMrkOH3VxUISBImkD1iXppDfH0AnI+Jo5pusE00++kDhgi+UnDIf8YuqRbTYquW/KXJHUTnz+Uj5W+haXBlwtfZWoAryFC8wHfOnVzxTn40BxwD6EZW3aNwqI6pY1PNXnYpu7AkfKLs30gfphZptJgWm0Px+UujF+Lz5+KQcJ2tm+BiW4hsSL4MvBhgt1rZDXiOpbmMdCg+WUk0u/QAfyM31mEMIbNegdt8KkwaLVB7xZZWr07+XBzla75pkO6awRpTwxAGuX+F/gK1OUGiyz7iE/UYcuO9M5eI0lPL92ym6OTPy5fI9zPbtKN1yvBr4GnSr653IDenm4BFyA6V6yT4FagRDiCj1SpkY4D78u8Ib0emuOM2DxRJE/IIyRLkf+Id9X5ekUFljG7+AKYhj9VcVS/jWpmBzNp4Fw7TPjzahaw53EIWtcMmX+qBsMXXcoExJaWVYC1OcSFljm7+yr7z3WEOSzEdTkMqexFS0YexD5BaLF5akZnOS8APwKlCo7FYLBaLJTf+B2tWnREOFMmfAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : - x_{2}, \\quad x_{1} : - x_{2}\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: -x_{2}, x_{1}: -x_{2}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx=getM('x',3,1)\n",
    "sp.solve((xa+2*sp.eye(3))*xx,xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\xi_1=\\begin{bmatrix}-1\\\\-1\\\\1\\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_2=\\lambda_3=1$时,解方程组$(A-E)x=0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJkAAAAWCAYAAADTuj63AAAABHNCSVQICAgIfAhkiAAAA+JJREFUaIHt2kmIHHUUx/HPGE1cghu4ZFzBICYKk5Me1DCoYFBE3Ah6kAa9GEUvHowHMwhBQYIbxhBBW5hLFEFRcD0oRDSIGFHclyAmKlExokYddTz8/8UUlarqqu6e6Z5QX2i6u+r93//3ul79l9dFQ8OAuQ5vYw/+xYrBymkYMiYwhe/xLE7PMzqwxMFyTOJ3PI2d0VlDQ8LrOFgYfC7HKM6u4+BmTOPWfitr2C95F/9hcfbEASWNjo7vH8+GoobatISbfnywMgr5BCM4KnuiLMkWxPepEptXhMCvzBwfQTueu7eqynnCfIx5LjQnebKg1CrDhM53zpiwIfgo43xDbLu5TocFtKOvVh989YO5iDmPlu5Hsrm8TqfWafRAbHROReet+P3O+H2L8pGyKln/w0Db7MacR0tv02Xb7GreFP2NVW0wYmYhd3wH2xOxFztwS+zoJSzsQmgeS3AGjuiTv34w2zHn0dJbks225juiz9s6Ga4ShtB3YoNNFTu4J9pP400cWmC3Bl/jTyGJz6/ov1t2pHRVeU3W8N0p5pV4HruizdWzqLvdJ82wVrj+v2J3jOGsCr5PiLqn8BTWi1Nntk62ykwmfoiHK4rfnfp8A/7IsVmNB4VE24qb8KJQj/umYj91+VJI6KrsqmHbKebD8D6ewDM1/BKWKkdmjiW1qCeFi5lme0W/Va7TODYKiTaCu/GacJ1+LvG9U0ji+3FNPPZqjlZwuBDMXnyr827hWmFa/U64Qx4tsNuGxzLHPo/C5htVY06oO5Ll0dLbdFlXc8JiYdNwWQe7S6PfrTgTB1VxPhkblQ2Vl+BvfIBjhJralLCGSrMQ/5jJ8IRH8EYVMUNE1ZjTDDrJutGcsCT2e24Hu2SnelEdYetjo5UF588ThtuvohDCDzkt/I+VZrTA1134tIOOYVr414k5zSCTrFvNCVvwns4z2uPR59I64iYUBzWGX4Sh97TMuWTTkF7Uj+Ycg3VCpbiMtuEoYdSNOc2gkqwXzXBfbFslcdoK6mRl9ZHpguNL8XI8f7GwuE6zNiUw4UdhXs+WQ47FDyUahoVuYh40vWregOtxIb6o0W9R3uSS1D2uqNOohG32rSx/Zn4u/OvSj5FsLnlIuPmX12jznBDncXU6uio2egEn6b0qvFpYfN6IZcI2/Tec0qPfYWWxUHZYIfyOt8fPJw9SVAU2CjWyC4SZJ3nt83RFZJHweM+e+KqVJ4cIu5F00a/XhxbXCHWTv4RibNGmYn9gXG+F00FRVPCdyLGdyNisy3M40qHDRUJGLxMyebPmwcWGGcaF3etPeEv1onBDQ0PDPON/y09QbaFP9l0AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : - x_{1} + x_{2}\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: -x_{1} + x_{2}}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx=getM('x',3,1)\n",
    "sp.solve((xa-sp.eye(3))*xx,xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "得$\\xi_2=\\begin{bmatrix}-1\\\\1\\\\0\\end{bmatrix},\\xi_3=\\begin{bmatrix}1\\\\0\\\\1\\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其中$\\xi_2\\xi_3^T\\not=0$,需要正交化:\n",
    ">$\\eta_2=\\xi_2,\\eta_3=\\xi_3-\\frac{[\\xi_3,\\eta_2]}{[\\eta_2,\\eta_2]}\\eta_2=\\begin{bmatrix}.5\\\\.5\\\\1\\end{bmatrix}$\n",
    "\n",
    "此时$\\xi_1\\bot\\eta_2,\\xi_1\\bot\\eta_3,\\eta_2\\bot\\eta_3.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单位化$P_1=\\frac{\\xi_1}{|\\xi_1|},P_2=\\frac{\\eta_2}{|\\eta_2|},P_3=\\frac{\\eta_3}{|\\eta_3|}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "于是正交阵$P=[p_1,p_2,p_3]$,使$P^{-1}AP=\\Lambda$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**步骤:**把对称阵$A$对角化的步骤为:\n",
    ">1. 求出$A$的所有各不相同的特征值$\\lambda_1,\\dots,\\lambda_s$,它们的重数依次为$k_1,\\dots,k_s(k_1+\\dots+k_s=n)$.\n",
    "2. 对每个$k_i$重特征值$\\lambda_i$,求方程组$|A-\\lambda_iE|=0$的基础解系,得$k_i$个线性无关的特征向量.<br>\n",
    "把这$k_i$个线性无关的特征向量正交化、单位化,得到$k_i$个两两正交的单位特征向量.<br>\n",
    "因为$k_1+\\dots+k_s=n$,总共可得$n$个两两正交的单位特征向量.\n",
    "3. 这$n$个两两正交的单位特征向量构成正交阵$P$,便有$P^{-1}AP=\\Lambda.\\Lambda$中对角元的排列次序应该与对应的列向量的排列次序相对应."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设$A$矩阵如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}2 & -1\\\\-1 & 2\\end{matrix}\\right], \\quad \\left[\\begin{matrix}2 & -1\\\\-1 & 2\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡2   -1⎤  ⎡2   -1⎤⎞\n",
       "⎜⎢      ⎥, ⎢      ⎥⎟\n",
       "⎝⎣-1  2 ⎦  ⎣-1  2 ⎦⎠"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[2,-1],[-1,2]])\n",
    "xa,xa.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求$A^n,n\\in\\mathbb{Z+}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解:**\n",
    "因为$A$是对称阵,所以$A$可以对角化."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAVCAYAAAAU9vPjAAAABHNCSVQICAgIfAhkiAAAAe5JREFUWIXt2L1rFEEYx/GPwcJC8RUTBBECEUFBERtB8NKITUDE0iKN/0H+gLMQopa+lCKijY1CGjvPFCJYRCxUECEpBMWIqKAiSiyePVyT3bvd3F087u4Lw+zNzDPz7O92Xp5hQCEqWEqlV//Vm7Vjh3/fe6lesT6j8SPUsLis/AyO4xAOYhPu4Gzb3W2diziCveLlv2MB93EVH1Ntv+F88jyJPVkdVoRq1ZwBnyX1X/Eyeb69avc7y088wQ1M4wqeCp/fYneOXU3qy0lT0ViccYxhXaptt4qzIaf8gvD7ek59TUqcoRIDPsRrOcp2GT9yyu8m+ViRTsqI0wtMJPnzIo2zFuReYgobsVks0MeEMNNFjPtBnOHU7wdiR/pQxLjXp9WI2EBGcBqjmMPhIsa9Lk6d97iHE9iOW0WM+kWcOgt4gf3icNiQfhMHdiX572YNOy3OTXEumuzwOGn2iTVmOUPiELgTj/GpWUdldqtTSZIa/KgQgIjFpjIcgl8lxmmVk7iMWbwRcdSwiAtH8Q7nynZa0Th8qFoZvabTfIbNHL5ga1lnWuAArolYcFH8MZ9FbFXFtga2NauMrcqyRczrS23qby2oaSJOu+5zJkSMkzX/u4lC9znz/t5rsPI+pywz8qPjbiJ9nzNgQBv4A9l/cGznV8fbAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left [ 1, \\quad 3\\right ]$$"
      ],
      "text/plain": [
       "[1, 3]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve((xa-x*sp.eye(2)).det(),x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求得$A$的特征值$\\lambda_1=1,\\lambda_2=3.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\Lambda=diag(1,3),\\Lambda^n=diag(1,3^n)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面求满足$P^{-1}AP=\\Lambda$的可逆矩阵$P$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_1=1$时,解方程组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFMAAAAWCAYAAAC8J6DfAAAABHNCSVQICAgIfAhkiAAAAwVJREFUWIXtmEtoFEEQhr81mqgExeD7fRCJD9ic9KJhEd8exBdBTzkoQhQF8aCCmIMSQYQgiEEvOeTi46CYg3pSUDSoIAgqihjBV1APATXqRtdD1eDaO9PbvbPrEpIfhtnp+rv6n97qrpqGIfw3bAPuAb3AL6CuvHLKgmYgDXwALgNzo4jDLU7mAx3AV+Ai8FYdDjbcBEYigbQemAos8nWyC8gAe4qpbIDjIfAbqA4zDrN0rNH702IrGsB4BiSAcWFG22RW6D1t4dxAonej0Z4A2tV23EVlkVBqPcFcVFhZIWjWwVMWThJJTE+MAU5q37O+g4agXX01OnBLrSfQMtu3Y6t2XOw4QKM+H9Ln89gj3xWmf19+MfW0qa+kT6cEfzfbyXm404E+oBvYrYNdAyo9hUZhClALjHXkl1LPAfW314W8GlkS97VTm+MgLcrPAHeA0RG8JuAV8B35s5Y6+veFi5564CrwTnmbHfxOQ/6kNHABOIZlyQdLOwM8BhY4it+X1a82gtOgInYA84BTwBdgpuMYPnDRswY4iiQr18kE2Al8y/KfspHHIMVpH/CG/JlrK7IdvFfnZyJ4XcA5o+0FEkXFhKuebLhO5jrl3kYCbYSrqA7tuNDCWQv8RKJ4AlKTpsmNhkqgH9hitJ8GbrkKcoCrHhOukxlUBcvDjLbs9lrvNRH2JcAlJHpXAh+Bw8gnqlnLjUcivMdo7yF/gnNNQD56CkVQrHeHGW2TmbZwkkAncgCyAllSIC/zANkmwpJLxnhOhLSZaEEibIOFU6geXwRz0W8zhiHqJecA19W+Cnhp2A/q/URW2yekmDajcCK50eqLQvTERb4AyEFQU9kiwgdd5H6BPKf4CagQuO6ZV5Q7yXeATdqxE5hB/K+HBiQ5bEdKo1akNJoV02+hqEaO1eqQ99yvv8NKtSrk2K1XL++5GIVkxUzWFfdwuAnZvH8gRXt9TH9xkOLfdwuudoPXbNiPRDlM5BmwCliGRFI1skwH2wFxCqkUPgN3gUdlVTOEIXjjDyOY2wB6+ZtBAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : x_{1}\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: x_{1}}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx=getM('x',2,1)\n",
    "sp.solve((xa-sp.eye(2))*xx,xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求得基础解系$p_1={[1,1]}^T.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\lambda_2=3$时,解方程组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGsAAAAWCAYAAADHA2ITAAAABHNCSVQICAgIfAhkiAAAAzRJREFUaIHtmUloVEEQQN8YTVyCYnDfDyJukJz0oiGIS9CDuBH0FNCLUfTiQQUxF4kgoiJi0INzyMUNFANuF4WIBhUURUURo5hoUA8BNepE46FqyNj5S/fMHzKJ82D4/3dXV3f97uqumg95BgybgHtAB/AbKOvb4QxYaoEE8BG4BMzyEhocoGAu0AB8A84DraosT/TcAoYizrAamAQscFGwDegGdkQ9sjyBPAT+AMVmxaCARiV6fZ6NEeXx5QUQA0abFUGTVaDXRIDMDcT71hrlMSCudQdtR9lPyLbNyfddEChlUKsdVwTIlCKBxzND+WFte8qlQx/iqqs6Al1RkG2b46pnhkujo9pooaXyan3eq89nCfZcW0z9uUCc7Nlcr7pKbRvE6DnoJoTITgE6gRZgu3Z0DShMY6BeTARmA6Mi0hcF2bR5t+rbGSZYibjzfW1Qb9lBncp3A3eA4T5yNcAb4AeyGBZb6k+XlpRx2fwaHHSH2VwOXAHaVGa9pd7JOu4EcA44gG6JZp5VSc+MPgWOW3bwKeV+M/DdQ6YKOIZMWBOwFbiK5HPvLPtx5TWyMGxpc5ANs3kE8Bg4A1x00NuKLIQjwAYtu4lMYC9GIolZJ/Ce8KhkI7JdfkBW0EkfuWbgtFH2SgfW37C1OYmLZ61S+SZgHjDEplGDNpofILMS+AU8AcYiOVkCOWNSKQS66FkpSU4At20Gk0PY2pyKy2Qlo8qlZkVQ5PJWryU+9YuAC4j3LUe2hX3I1mrmGWMQD203ytsJD2ByKcBwsTldkslwi1kRNFmJAJlSoBH5g3cZsh2AGPIA2Ua9godu4znmUWZSh6zeNSFy2SZdm11Jvu8uvwov/F7iTOC61q9ADvFU9uj1UErZZySRNL1oHL29LRdJx+ZMCVvE/5CM96Na0c30zu5f0j8DDFdczqzLKj/epYN12qgRmErmmXkVcjBvAeYg/5B8BaZnqDdXKUY+eZQh73GX3k/zkS9CPot06M/pfQ9DIp7UpDHTj481yMH5E0mKyzPUl8tU4J14xz1kaw2Z/V4KYyEdFgFLEE8oRrax/AfI6KlAIs0vwF3gUZ+OJk+e/4q/6Nfo5lk7sWIAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : - x_{1}\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: -x_{1}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve((xa-3*sp.eye(2))*xx,xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求得基础解系$p_2={[1,-1]}^T.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "于是$Ap_1=p_1,Ap_2=3p_2$<br>\n",
    "若$P=[p_1,p_2]=\\begin{bmatrix}1&1\\\\1&-1\\end{bmatrix}$,则<br>\n",
    "$P^{-1}=$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{1}{2} & \\frac{1}{2}\\\\\\frac{1}{2} & - \\frac{1}{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1/2  1/2 ⎤\n",
       "⎢         ⎥\n",
       "⎣1/2  -1/2⎦"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xp=sp.Matrix([[1,1],[1,-1]])\n",
    "xpi=xp.inv()\n",
    "xpi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$P^{-1}AP=\\Lambda$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0\\\\0 & 3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  3⎦"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xp*xa*xpi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "即$A=P\\Lambda P^{-1},A^n={(P\\Lambda P^{-1})}^n=P\\Lambda^n P^{-1}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.二次型及其标准形\n",
    "**例:**<br>\n",
    "解析几何中,二次曲线的一般形式$ax^2+bxy+cy^2=0$,通过选择适当的旋转变换$\\begin{cases}x=x^{'}cos\\phi-y^{'}sin\\phi\\\\y=x^{'}sin\\phi+y^{'}cos\\phi\\\\\\end{cases}$,使得$mx^{'2}+ny^{'2}=0$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**含有$n$个变量$x_1,\\dots,x_n$的二次齐次函数$f(x_1,\\dots,x_n)=\\sum_{i,j}^{i=n,j=n}a_{ij}x_ix_j$,称为二次型."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "令$X=\\begin{bmatrix}x_1\\\\\\vdots\\\\x_n\\end{bmatrix},A=\\begin{bmatrix}a_{11}&\\dots&a_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{n1}&\\dots&a_{nn}\\end{bmatrix}$,则$f(x_1,\\dots,x_n)=\\sum_{i,j}^{i=n,j=n}a_{ij}x_ix_j=X^TAX$,其中$A$为二次型的矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "令$A=\\Lambda$(对称阵),则$f(x_1,\\dots,x_n)$为对称阵的二次型,其中对称阵$A$的秩也叫做二次型$f$的秩."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于二次型,需找可逆的线性变换<br>\n",
    "$\\begin{cases}x_1=c_{11}y_1+\\dots+c_{1n}y_n,\\\\\\vdots\\\\x_n=c_{n1}y_1+\\dots+c_{nn}y_n.\\end{cases}$<br>\n",
    "简记为$X=CY$,于是$f=X^TAX={(CY)}^TA{(CY)}=Y^T(C^TAC)Y$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**设$A,B$都是$n$阶矩阵,若有可逆矩阵$P$满足$P^{-1}AP=B$,则称矩阵$A和B$相似."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**设$A,B$都是$n$阶矩阵,若有可逆矩阵$C$满足$C^{T}AC=B$,则称矩阵$A和B$合同."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**合同的性质:**\n",
    "> 1. 合同关系为等价关系\n",
    "2. 与对称矩阵合同的矩阵也是对称矩阵\n",
    "3. 合同矩阵具有相同的秩"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**显然,**\n",
    ">* $若A为对称阵,B^T={(C^TAC)}^T=C^TA^T{(C^T)}^T=C^TAC=B\\Rightarrow B也为对称阵.$\n",
    "* $R(B)=R(A).$\n",
    "\n",
    "经过可逆变换后,二次型$f$的矩阵由$A$变为与$A$合同的矩阵$C^TAC$,且二次型的秩不变."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**如果二次型只含有平方项,即$f=k_1y_1^2+\\dots+k_ny_n^2$,称之为二次型的标准形(或法式).如果标准形的系数$k_1,\\dots,k_n$只在$(-1,0,1)$三个数中取值,则上式称为二次型的规范形.(这里只讨论实二次型,所求线性变换也限于实数范围.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二次型$f$经过可逆变换$X=CY$变为标准形,即$f=X^TAX$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**如果$n$阶矩阵$A$满足$A^TA=E$,则称矩阵$A$为正交矩阵,简称正交阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$A为n$阶对称阵,则必有正交阵$P$,使得$P^{-1}AP=\\Lambda,其中\\Lambda$是以$A的n$个特征值为对角元的对角阵(不唯一)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**任给二次型$f(X)=X^T\\Lambda X$,总存在正交变换$X=PY$,使$f$化为标准$f(PY)=\\lambda_1y_1^2+\\dots+\\lambda_ny_n^2$,其中$\\lambda_1,\\dots,\\lambda_n是f$的矩阵$A$的特征值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**任给二次型$f(X)=X^TAX$，总存在可逆变换$X=CZ$,使得$F(CZ)$为规范形.\n",
    "\n",
    "**证明:**\n",
    ">$f(PY)=\\lambda_1y_1^2+\\dots+\\lambda_ny_n^2$若$R(A)=r$,不妨设$\\lambda_1,\\dots,\\lambda_r$不等于零,$\\lambda_{r+1}=\\dots=\\lambda_n=0$,令<br>\n",
    "$K=\\begin{bmatrix}k_1&&\\\\&\\ddots&\\\\&&k_n\\end{bmatrix}$,其中$k_i=\\begin{cases}\\frac{1}{\\sqrt{|\\lambda_i|}}&,i\\leq r,\\\\1&,i>r\\end{cases}$<br>\n",
    "则$K$可逆,变换$Y=KZ$把$f(PY)$化为$f(PKZ)={(PKZ)}^TA(PKZ)=Z^TK^TP^TAPKZ=Z^TK^T\\Lambda KZ$<br>\n",
    "其中$K^T\\Lambda K=diag\\begin{bmatrix}\\frac{\\lambda_1}{|\\lambda_1|},\\dots,\\frac{\\lambda_r}{|\\lambda_r|},0,\\dots,0\\end{bmatrix}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**用正交变换化二次型为标准形的具体步骤:**\n",
    ">1. 将二次型表示成矩阵形式$f=X^TAX$,求出$A$;\n",
    "2. 求出$A$的所有特征值$\\lambda_1,\\dots,\\lambda_n$;\n",
    "3. 求出对应于特征值的特征向量$\\xi_1,\\dots,\\xi_n$;\n",
    "4. 将特征向量$\\xi_1,\\dots,\\xi_n$正交化,单位化,得$p_1,\\dots,p_n$,记$P=(p_1,\\dots,p_n)$;\n",
    "5. 作正交变换$X=PY$,则得$f$的标准形$f=\\lambda_1y_1^2+\\dots+\\lambda_ny_n^2$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
